package com.swj.modules.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.swj.common.core.model.SysRole;
import com.swj.common.core.model.SysUser;
import com.swj.common.core.model.SysUserRole;
import com.swj.common.core.utils.SysStringUtil;
import com.swj.modules.system.mapper.SysRoleMapper;
import com.swj.modules.system.mapper.SysUserRoleMapper;
import com.swj.modules.system.service.ISysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @author Sans
 * @Description 角色业务实现
 * @CreateTime 2019/6/14 15:57
 */
@Service("sysRoleService")
@EnableAspectJAutoProxy(proxyTargetClass = true, exposeProxy = true)
public class SysRoleServiceImpl implements ISysRoleService {

    @Resource
    private SysRoleMapper roleMapper;

    @Resource
    private SysUserRoleMapper userRoleMapper;

    /**
     * 根据条件分页查询角色数据
     *
     * @param role 角色信息
     * @return 角色数据集合信息
     */
    @Override
    //@DataScope(deptAlias = "d")
    public List<SysRole> selectRoleList(SysRole role) {
        return roleMapper.selectList(new QueryWrapper<>());
    }

    /**
     * 根据用户ID查询权限
     *
     * @param userId 用户ID
     * @return 权限列表
     */
    @Override
    public Set<String> selectRolePermissionByUserId(String userId) {
        List<SysRole> perms = roleMapper.selectRolePermissionByUserId(userId);
        Set<String> permsSet = new HashSet<>();
        for (SysRole perm : perms) {
            if (SysStringUtil.isNotNull(perm)) {
                permsSet.addAll(Arrays.asList(perm.getRoleKey().trim().split(",")));
            }
        }
        return permsSet;
    }

    @Override
    public List<SysRole> selectRoleAll() {
        //return SpringUtil.getAopProxy(this).selectRoleList(new SysRole());
        return this.selectRoleList(new SysRole());
    }

    /**
     * 根据用户ID查询角色
     *
     * @param userId 用户ID
     * @return 角色列表
     */
    @Override
    public List<SysRole> selectRolesByUserId(String userId) {
        List<SysRole> userRoles = roleMapper.selectRolePermissionByUserId(userId);
        List<SysRole> roles = selectRoleAll();
        for (SysRole role : roles) {
            for (SysRole userRole : userRoles) {
                if (role.getId().equals( userRole.getId())) {
                    role.setFlag(true);
                    break;
                }
            }
        }
        return roles;
    }

    /**
     * 查询角色集合
     *
     * @param userId 用户id
     * @return 角色列表
     */
    @Override
    public List<SysRole> selectRoleByUserId(String userId) {
        return roleMapper.selectRoleByUserId(userId);
    }

    /**
     * 添加user-role的关联关系
     *
     * @param user user信息
     */
    @Override
    public void insertUserRoleRelation(SysUser user) {
        if (SysStringUtil.isNotEmpty(user.getRoleIds())) {
            // 新增用户与角色管理
            for (String roleId : user.getRoleIds()) {
                SysUserRole ur = new SysUserRole();
                ur.setUserId(user.getId());
                ur.setRoleId(roleId);
                userRoleMapper.insert(ur);
            }
        }
    }

    /**
     * 删除user-role的关联关系
     *
     * @param user user信息
     * @return 删除数据库行数
     */
    @Override
    public int deleteUserRoleRelation(SysUser user) {
        QueryWrapper<SysUserRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(SysStringUtil.isNotEmpty(user.getId()), "user_id", user.getId());
        return userRoleMapper.delete(queryWrapper);
    }

    /**
     * 删除user-role的关联关系
     *
     * @param userIds 用户id集合
     * @return 删除数据库行数
     */
    @Override
    public int deleteUserRoleRelations(String[] userIds) {
        QueryWrapper<SysUserRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.in(userIds.length > 0, "user_id", Arrays.asList(userIds));
        return userRoleMapper.delete(queryWrapper);
    }
}